package com.halsys.symmht;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import javax.mail.MessagingException;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;

import com.hankenindustries.mht.*;

import android.util.Log;
import android.webkit.WebView;
import android.widget.TextView;
import android.content.Intent;

public class SymmhtActivity extends Activity {

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        Uri contentUri = intent.getData();
        TextView tv = new TextView(this);
        String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
        if (!sdcard.endsWith("/"))
        {
        	sdcard = sdcard + "/";
        }
        Collection<Attachment> attachments;


        //  Now extract the HTML and embedded objects:
        String unpackDir = sdcard + "mhtview/";
        //  Extract to /mhtview
        File uDir = new File(unpackDir);
        if (!uDir.exists())
        {
        	uDir.mkdir();
        }
        try
        {
        	attachments = MHTUnpack.unpack(getContentResolver().openInputStream(contentUri));
        }
        catch (Exception e)
        {
        	tv.setText("unpack " + e.getMessage());
        	setContentView(tv);
        	return;
        }
		if (attachments == null)
		{
        	tv.setText("Attachments is null");
        	setContentView(tv);
        	return;
		}
        try
        {
        	for (Attachment attachment : attachments) {
        		if (attachment == null)
        		{
                	tv.setText("Attachment is null");
                	setContentView(tv);
                	return;
        		}
        		String filename = attachment.getFileName();
        		String path = unpackDir;
        		if (filename == null || filename.equals(""))
        		{
            		if (attachment.getMimeType().equals("application/octet-stream"))
            		{
            			path = unpackDir + "bogus.html";
            		}
            		else
            		{
            			path = unpackDir + "index.html";
            		}
        		}
        		else
        		{
        			path = unpackDir + filename;
        		}
        		File newFile = new File(path);
        		if (newFile.exists()) {
        			newFile.delete();
        		}
        		attachment.saveFile(path);
        	}
        }
        catch (MessagingException me)
        {
        	tv.setText("saveFile MessagingException");
        	setContentView(tv);
        	return;
        }
        catch (IOException ie)
        {
        	tv.setText("saveFile IOException");
        	setContentView(tv);
        	return;
        }
        WebView webview = new WebView(this);
        webview.loadUrl("file://" + unpackDir + "index.html");
        setContentView(webview);
    }
}